// import 'dart:io';

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:zootix/models/zt_app_bottom_nav_item.dart';
import 'package:zootix/services/zt_app_info.service.dart';
import 'package:zootix/util/zt_screen_adaper_util.dart';

import '../../util/zt_util.dart';

class ZtLandingPage extends StatefulWidget {
  ZtLandingPage({Key? key}) : super(key: key);

  @override
  _ZtLandingPageState createState() => _ZtLandingPageState();
}

class _ZtLandingPageState extends State<ZtLandingPage> {
  int _selectedLanding = 0;

  List<BottomNavigationBarItem> bottomNavItems = [];
  List<Widget> pages = [];

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: const Size(414, 896),
      orientation: Orientation.portrait,
      context: context,
    );
    List<AppBottomNavItem> roleWithBottomNavItems =
        ZtAppInfoService.to.bottomNavItems!;
    bottomNavItems = roleWithBottomNavItems
        .map((e) => BottomNavigationBarItem(
            icon: Image.asset(
              e.icon!,
              height: ZtScreenAdaper.width(30),
              width: ZtScreenAdaper.width(30),
            ),
            activeIcon: Image.asset(
              e.activeIcon!,
              height: ZtScreenAdaper.width(30),
              width: ZtScreenAdaper.width(30),
            ),
            label: e.label))
        .toList();
    pages = roleWithBottomNavItems.map((e) => e.page!).toList();
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        iconSize: ZtScreenAdaper.sp(25),
        type: BottomNavigationBarType.fixed,
        items: bottomNavItems,
        showSelectedLabels: false,
        showUnselectedLabels: false,
        currentIndex: _selectedLanding,
        onTap: (landing) {
          _onItemTapped(landing);
        },
      ),
      body: pages[_selectedLanding],
    );
  }

  void _onItemTapped(int landing) {
    ZtLogManger().putLog(
      ZtLogType.clickEvent,
      ZtLogManger.buildEvent(
          logType: ZtLogType.clickEvent,
          page: 'tab${ZtAppInfoService.to.bottomNavItems![landing].label}'),
    );
    if (landing != _selectedLanding) {
      setState(() {
        _selectedLanding = landing;
      });
    }
  }
}
